import csv
from datetime import datetime
import re


def extract_time_from_string(string):
    # 定义时间模式的正则表达式
    pattern = r"\d{4}年\d{2}月\d{2}日"

    # 使用正则表达式匹配时间模式
    match = re.search(pattern, string)

    if match:
        time = match.group()
        return time.replace("年", '-').replace('月', '-').replace('日', '')
    else:
        return None


with open('result_1.csv', 'r', encoding='utf-8') as input_file:
    # 创建CSV读取器
    reader = csv.reader(input_file, delimiter=',')

    # 创建新的CSV文件
    with open('result_2.csv', 'w', encoding='utf-8', newline='') as output_file:
        # 创建CSV写入器
        writer = csv.writer(output_file, delimiter='\t')

        # 遍历每一行数据
        num = 0
        for row in reader:
            num += 1
            if num == 1:
                continue

            # 写入新的CSV文件
            # 标签处理
            label = " ".join(row[3].split(","))
            if len(label) == 0:
                num += 1
            # print(src_label)

            # 9.提取时间
            src_data = row[9]
            date = extract_time_from_string(src_data)
            list = [row[1], row[2], label, row[4], row[5], row[6], row[7], row[8], date, row[10], row[11], row[12]]
            print(list)
            writer.writerow(list)

        print(num)  # 4128
